package com.LeetCode.Array;

import java.util.*;

/**
 * 合并区间
 */
public class MegreArray {
    public int[][] merge(int[][] arr) {

        Arrays.parallelSort(arr,Comparator.comparingInt(x -> x[0]));

        LinkedList<int[]> list = new LinkedList<>();
        for(int i=0; i<arr.length; i++){
            if(list.size() == 0 || list.getLast()[1] < arr[i][0]){
                list.add(arr[i]);
            }else{
                list.getLast()[1] = Math.max(list.getLast()[1],arr[i][1]);
            }
        }

        int[][] ans = new int[list.size()][2];
        int index = 0;
        for(int[] arr1 : list){
            ans[index++] = arr1;
        }
        return ans;
    }
}
